package main

import (
	"errors"
	"fmt"
)

type UserError struct {
	code int
	msg  string
}

func NewUserError(code int, msg string) UserError {
	return UserError{code, msg}
}

func (e UserError) Error() string {
	return fmt.Sprintf("user code %d msg %s", e.code, e.msg)
}

func main() {
	//saveOrder()
	var (
		name   string
		passwd string
	)

	fmt.Println("请输入用户名")
	_, err := fmt.Scanln(&name)
	if err != nil {
		return
	}

	fmt.Println("请输入用户密码")
	_, err = fmt.Scanln(&passwd)
	if err != nil {
		return
	}
	err = login(name, passwd)
	if err != nil {
		fmt.Println("登录错误了", err)
		return
	}

}

func saveOrder() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Printf("%+v", err)
		}
	}()
	panic("数组越界了")
}

func login(name, passwd string) error {
	if name == "" || passwd == "" {
		//errors.New("name和passwd 不能为空")
		//fmt.Errorf("name和passwd 不能为空")
		return errors.New("name和passwd 不能为空")
	} else {
		if name == "zs" {
			return NewUserError(2000, fmt.Sprintf("%s用户已禁用", name))
		}

		fmt.Printf("用户%q登录成功!", name)
		return nil
	}
}
